﻿<?php
	// Here local values are declared 
	ini_set('max_execution_time', 59);
	require_once('/var/www/cpa/_inc/app.php');
	$dbCompanies = Db::getModel('companies');
	$array_id_company = file("/var/www/cpa/_inc/cron/company.db");
	$num_company = (int)end($array_id_company);

	$end = time() + 59;
	$random_time = 1;
	$rand[0] = 1;
	$rand[1] = 10;
	
	/**
	* This cycle executes queries based on timer. 
	*/
	while((time() + $random_time) < $end){
		do{
			$num_company++;
		} while(in_array($num_company,$array_id_company));
		
		$data = file_get_contents("http://www.scambook.com/company/view/".$num_company."/");
		
		// Here a title is parsed
		preg_match('/itemprop="name">([^<]*)/', $data, $title);
		$title = str_replace("&#039;", "'",htmlspecialchars_decode(trim($title[1])));
		
		// Here a phone number is parsed
		preg_match('/"tel">([\d\+\(\)\s\-]+)/', $data, $phone);
		$phone = ($phone[1]);
		
		// Here an email is parsed
		preg_match('/itemprop="email"[\s]+href="mailto:([0-9a-zA-Z]([\-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][\-0-9a-zA-Z]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,})/', $data, $email);
		$email = trim($email[1]);
		
		// Here a website's address is parsed
		preg_match('/url">\s*<a.*target="_blank">(.*)<\/a>/', $data, $site);
		$site = trim($site[1]);
		
		// Here an address is parsed
		preg_match('/<ul\s*itemprop="address"[^<](.*)<\/ul>/isU', $data, $address);
		$address = preg_replace('/[ \f\n\r\t\v]{2,}/',' ',$address);;
		$address = trim(strip_tags($address[0]));
		
		if($dbCompanies->countBy(array("title"=>"='".mysql_escape_string($title)."'")) == 0 and !empty($title)){
			$data = array();
			$data['title'] = $title;
			if(!empty($phone))
				$data['phone'] = $phone;
			if(!empty($email))
				$data['email'] = $email;
			if(!empty($site))
				$data['site'] = $site;
			if(!empty($address))
				$data['address'] = $address;
			$data['description'] = "";
			$data['cdate'] = _dformat(time(),_cfg('format.datetime.sql'));
			// Here data is inserted into the database.
			$dbCompanies->insert($data);
			// Here data is recorded to the log. 
			file_put_contents("/var/www/cpa/_inc/cron/company_record.db","\n".date("H:i:s")." : ".$num_company." : ".$title." : ".$phone." : ".$email." : ".$site." : ".$address,FILE_APPEND);
		}

		sleep($random_time);
		$random_time = rand($rand[0],$rand[1]);
		// Here processed company IDs are recorded in the company DB file  
		file_put_contents("/var/www/cpa/_inc/cron/company.db","\n".$num_company,FILE_APPEND);
	}
	exit;
?>
